package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021-7-17.<br><br>
 *
 * 简单：重复的子字符串
 */
public class Topic459 {

    public static void main(String[] args) {
        Topic459 instance = new Topic459();

        System.out.println(instance.repeatedSubstringPattern("abab"));
        System.out.println(instance.repeatedSubstringPattern("aba"));
        System.out.println(instance.repeatedSubstringPattern("abcabcabcabc"));
    }

    public boolean repeatedSubstringPattern(String s) {
        if(s.length() == 1) {
            return false;
        }

        int len = s.length();
        char[] array = s.toCharArray();

        for(int t = 1; t <= len / 2; t++) {
            if(len % t != 0) {
                continue;
            }

            int i = t;
            for(; i <len && array[i % t] == array[i]; i++);

            if(i == len) {
                return true;
            }
        }

        return false;
    }

    public boolean repeatedSubstringPattern4(String s) {
        if(s.length() == 1) {
            return false;
        }

        String temp = s + s;
        return temp.substring(1, temp.length() - 1).contains(s);
    }

    public boolean repeatedSubstringPattern3(String s) {
        if(s.length() == 1) {
            return false;
        }

        String temp = s + s;
        return temp.substring(1, temp.length() - 1).contains(s);
    }

    public boolean repeatedSubstringPattern2(String s) {
        for(int i = 1; i <= s.length() / 2; i++) {
            if(s.length() % i == 0) {
                int count = s.length() / i;
                String subString = s.substring(0, i);
                StringBuilder builder = new StringBuilder();
                while(count-- > 0) {
                    builder.append(subString);
                }

                if(builder.toString().equals(s)) {
                    return true;
                }
            }
        }

        return false;
    }
} 
